import requests,time,re
from lxml import etree
import urllib.parse
ip = []
def zdaye():
    #采集过程封ip
    headers = {
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "www.zdaye.com",
            "Referer": "https://www.zdaye.com/Help.html",
            "sec-ch-ua": '''"Chromium";v="21", " Not;A Brand";v="99"''',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '''"Windows"''',
            "Sec-Fetch-Dest": "document",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}

    for i in range(10):#10页
        url = f'https://www.zdaye.com/free/{i+1}'
        response = requests.get(url=url,headers=headers)
        print(response.text)
        result = etree.HTML(response.content)
        try:
            for n in range(20):
                    ip = result.xpath(f"/html/body/div[3]/div/table/tbody/tr[{n + 1}]/td[1]/text()")[0]
                    port = result.xpath(f"/html/body/div[3]/div/table/tbody/tr[{n + 1}]/td[2]/text()")[0]
                    ip_port = f"{ip}:{port}"
                    print(ip_port)
            print('____________')
        except Exception:
            print(f"请求发生异常")
        time.sleep(10)

def kxdaili():
    for i in range(10):#10页
        url = f'http://www.kxdaili.com/dailiip/1/{i+1}.html'
        response = requests.get(url=url)
        pattern = r'<td>([\d.]+)</td>'#匹配出<td></td>之间的内容，并只保留其中是数字和小数点.的部分
        results = re.findall(pattern, response.text)
        print(results)
        for n in range(10):
            try:
                ip_temp = results[2*n] + ":" + results[2*n+1]
                #print(ip_temp)
                ip.append(ip_temp)
            except Exception:
                print("没了")
                break
        #xpath报错，奇葩，浏览器调试正常
        #ip = result.xpath(f"/html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[{n + 1}]/td[1]/text()")[0]
        #port = result.xpath(f"/html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[{n + 1}]/td[2]/text()")[0]

        time.sleep(5)
    print(ip)
    print(len(ip))

def ceshi():
    #ip = ['47.100.90.127:4444', '47.96.70.163:8888', '117.74.65.207:8118', '124.70.205.56:8089', '39.104.62.128:9999', '116.63.130.30:1081', '121.37.201.60:8080', '112.124.2.212:8888', '120.79.31.133:52869', '120.31.52.68:8118', '8.134.138.108:8888', '8.213.128.6:808', '120.46.215.52:3000', '124.70.221.252:8080', '122.9.151.210:3132', '139.224.56.162:1234', '8.219.169.172:20', '58.220.95.30:10174', '139.196.151.191:8080', '123.57.1.16:59394', '39.104.57.170:10001', '115.182.212.177:80', '120.79.7.173:8888', '8.134.140.146:9999', '8.130.39.117:8080', '47.113.224.182:83', '8.209.253.237:8999', '39.104.26.204:8889', '101.132.25.152:50001', '116.63.128.247:8889', '120.46.197.14:8083', '8.219.74.58:1000', '115.29.149.2:8282', '139.196.214.238:2087', '121.37.203.216:3128', '47.109.53.253:45554', '47.106.144.184:7890', '139.9.119.20:80', '47.113.219.226:9091', '123.60.139.197:6969', '47.98.134.232:9992', '117.74.65.29:8181', '47.99.180.88:7890', '101.200.235.69:9000', '47.92.248.86:10000', '139.196.78.175:7890', '120.79.21.48:3127', '47.109.46.223:5678', '47.109.57.93:6969', '139.129.231.228:5001', '123.60.109.71:8090', '120.79.16.132:8080', '8.130.34.44:1234', '8.219.5.240:8080', '116.62.50.250:7890', '121.37.207.154:8999', '120.79.34.201:30001', '47.92.247.250:10000', '8.212.23.2:80', '39.100.120.200:7890', '120.55.49.231:20000', '123.57.1.78:10443', '121.40.115.140:8080', '115.29.148.215:8118', '101.200.187.233:19', '122.9.131.161:3128', '8.213.128.90:8080', '123.56.129.203:50001', '124.71.157.181:8888', '101.34.72.57:7890', '8.130.36.245:8080', '8.219.43.134:20201', '121.37.199.23:8089', '39.104.79.145:8499', '47.113.203.122:41890', '8.208.84.236:8080', '47.92.248.197:41890', '8.134.136.224:8080', '47.113.221.120:1080', '47.92.242.45:8999', '8.134.139.219:8080', '61.130.9.37:443', '8.130.34.237:8080', '140.210.196.193:8060', '47.92.239.69:8081', '47.113.230.224:3333', '115.29.151.41:8081', '8.213.137.155:80', '8.208.90.243:8999', '139.198.168.65:7890', '117.74.65.215:9443']
    url = 'http://httpbin.org/get?show_env=1'
    for i in range(len(ip)):
        ip_temp = ip[i].split(":")
        proxies = {
            'http': ip[i],
            #'https': 'http://60.182.184.172:8888'
        }
        try:
            response = requests.get(url, proxies=proxies, timeout=5)
            #print(response.text)
            if response.text.find(ip_temp[0]) != -1:
                print(ip[i], "-----------匿名")
            else:
                print(ip[i], "-----------非匿名")
        except requests.Timeout:
            print(ip[i],"-----------请求超时")
        except:
            print(ip[i],"-----------请求发生异常")

#zdaye()
kxdaili()
ceshi()




